
#include "Question.h"

Question * creerQuestion()
{
    Question * Q = (Question*)malloc(sizeof(Question));
    initQuestion(Q);
    return Q;
}
void initQuestion(Question * CC)
{
	int i;
	CC->Quest = malloc (64 * sizeof (char));
	CC->tabRep = malloc(4 * sizeof(Reponse *));
	for ( i=0 ; i<4 ; i++ )
	{
		CC->tabRep[i] = creerReponse();
	}
}
Reponse * creerReponse()
{
    Reponse * R = (Reponse*)malloc(sizeof(Reponse));
    initReponse(R);
    return R;
}
void initReponse(Reponse * Rep)
{
	Rep->rep = malloc (64 * sizeof (char));
    Rep->VF = false;
}
void libereReponse(Reponse * Rep)
{
    free(Rep->rep);
    Rep->VF = false;
}
void detruitReponse(Reponse **Rep)
{
    libereReponse(*Rep);
    free(*Rep);
    *Rep = NULL;
}
void libereQuestion(Question * CC)
{
    int i;
    for ( i=0 ; i<4 ; i++ )
	{
		detruitReponse(&CC->tabRep[i]);
	}
	free(CC->Quest);
	free(CC->tabRep);
}
void detruitQuestion(Question ** CC)
{
    libereQuestion(*CC);
    free(*CC);
    *CC = NULL;
}



void setQuest(Question * CC, const char * texte)
{
	strcpy(CC->Quest, texte);
}
void setRep(Reponse * Rep, const char * texte, const bool vrfx)
{
	strcpy(Rep->rep, texte);
	Rep->VF = vrfx;
}
void setRepQuest(Question * Q, const char * texte)
{
    int i;
    int verif = 0;
    for(i=0;i<4;i++)
    {
        if(Q->tabRep[i]->VF == 2 && verif == 0)
        {
            if(i == 0)
            {
                setRep(Q->tabRep[i], texte,1);
            }
            else
            {
                setRep(Q->tabRep[i], texte,0);
            }
            verif = 1;
        }

    }
}
char * getQuest(const Question * CC)
{
	return CC->Quest;
}
char * getRep(const Reponse * Rep)
{
	return Rep->rep;
}
char * getRepJuste(const Question * CC)
{
    int i;
    int nb = 0;
    for(i=0;i<4;i++)
    {
        if(getVF(CC->tabRep[i]) == true)
            nb = i;

    }
	return getRep(CC->tabRep[nb]);
}
bool getVF(const Reponse * Rep)
{
	return Rep->VF;
}

void repAlea(const Question * quest, char * rep[4])
{
	int i, nbr;
	nbr = rand()%4;
	for(i=0;i<4;i++)
	{
		rep[i] = getRep(quest->tabRep[nbr]);
		nbr++;
		if(nbr == 4)
			nbr = 0;
	}
}
void repartirReponseAlea(Question *Q)
{
    int nb = 4;
    int i,j;
    Reponse *temp;

    for(i=0;i<nb;i++)
    {
       j = rand() % nb;
       temp = Q->tabRep[j];
       Q->tabRep[j] = Q->tabRep[i];
       Q->tabRep[i] = temp;
    }
}

